package work;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * @Author admin
 * @Date 2019/4/12 10:34
 */
public class Test2 {
    public static void main(String[] args) {
        /*循环读取：
         * 1）读取一行文本
         * 2）将该行文本进行分割，一部分做key，一部分作value
         * 3）将key，value保存在HashMap中*/

        /*2.遍历集合
         *
         * 读取文件时，若该文件编码是UTF—8，就不需要用转换流*/
        HashMap<Integer, String> hashMap = new HashMap<Integer, String>();


        Reader in = null;
        BufferedReader br = null;

        try {
            in = new FileReader("pan.txt");
            br = new BufferedReader(in);
            String line;
            while ((line = br.readLine()) != null) {
                String[] s = line.split("-");
                Integer key = Integer.valueOf(s[0]);
                hashMap.put(key, s[1]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Iterator iterator = hashMap .keySet().iterator();
        while (iterator.hasNext()){
            Integer key =(Integer) iterator.next();
            System.out.println(key+"--"+hashMap.get(key));
        }
        int a=(int)(1+Math.random()*3);
        System.out.println(hashMap.get(a));
    }
}
